package com.pacy.pacybaseproject.constviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;

import com.pacy.pacybaseproject.R;

import io.reactivex.annotations.Nullable;
import me.goldze.mvvmhabit.utils.StringUtils;

/**
 * @author PACY
 * @date 2022/4/24
 * description:
 */
public class TopTextView extends LinearLayout {
    String title,content;
    TextView titleTv,contentTv;

    public TopTextView(Context context) {
        super(context);
    }

    public TopTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        /**
         * 加载布局  获取自定义属性
         */
        LayoutInflater.from(context).inflate(R.layout.top_text_view, this, true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TopTextView);
        title = a.getString(R.styleable.TopTextView_topTextTitle);
        content = a.getString(R.styleable.TopTextView_topTextContent);
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        contentTv = findViewById(R.id.content);
        titleTv = findViewById(R.id.title);


        if (StringUtils.isNotEmpty(title)) {
            setTitleTv(title);
            setContentTv(content);
        } else {
            throw new NullPointerException("title is not empty");
        }
    }
    /**
     * 设置内容
     *
     * @param content
     */
    public void setContentTv(String content) {
        contentTv.setText(content);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitleTv(String title) {
        titleTv.setText(title);
    }

    @BindingAdapter("topText_content")
    public static void setStrContent(TopTextView commTextView, String content) {
        if (commTextView != null) {
            String mCurrentStr = commTextView.contentTv.getText().toString().trim();
            if (StringUtils.isNotEmpty(content)) {
                if (!content.equalsIgnoreCase(mCurrentStr)) {
                    commTextView.contentTv.setText(content);
                }
            }
        }
    }
}
